#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int n, a[200005];
int color[200005];

int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);

    int T;
    scanf("%d", &T);

    while (T--)
    {
        scanf("%d", &n);
        for (int i=1; i<=n; i++) scanf("%d", &a[i]);
        memset(color, 0, sizeof(color));

        int s = 0;
        for (int i=1; i<=n; i++){
            bool ruse=0, buse=0;
            for (int j=i+1; j<=n; j++){
                if (a[i] == a[j]){
                    if (!ruse || !buse){
                        if (!ruse) color[i] = color[j] = 1;
                        else if (!buse) color[i] = color[j] = 2;
                        s += a[i];
                    }
                }else{
                    if (color[j] == 1) ruse = 1;
                    if (color[j] == 2) buse = 1;
                }
            }
        }

        printf("%d\n", s);

    }
    

    fclose(stdin);
    fclose(stdout);
    return 0;
}